# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import json


class ItcastPipeline(object):
    def __init__(self):
        self.filename = 'itcast/ext/itcast.json'
        self.file = None

    def open_spider(self, spider):
        self.file = open(self.filename, 'w+', encoding='utf-8')

    def process_item(self, item, spider):
        _data_dict = dict(item)
        _str_dict = json.dumps(_data_dict, ensure_ascii=False) + ',\n'
        self.file.write(_str_dict)
        return item

    def close_spider(self, spider):
        self.file.close()
